<?php
class Administrator_calendar_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }		
	
	/*Content Management*/
	function content_management_add()
	{
		$calendar_type_id = $this->input->post('calendar_type_id');
		$calendar_activity_date_start = $this->input->post('calendar_activity_date_start');
		$calendar_activity_date_end = $this->input->post('calendar_activity_date_end');
		$is_publish = $this->input->post('is_publish');
		$calendar_subject = $this->input->post('calendar_subject');
		$calendar_brief = $this->input->post('calendar_brief');
		$calendar_detail = $this->input->post('calendar_detail');
		$calendar_place = $this->input->post('calendar_place');
		$calendar_link = $this->input->post('calendar_link');
		$uploadify_store = explode(',',$this->input->post('uploadify_store'));
		$datetime = date('Y-m-d H:i:s');
		
		//Insert calendar
		$data = array(
			'calendar_activity_date_start' => convertDatetime($calendar_activity_date_start),
			'calendar_activity_date_end' => convertDatetime($calendar_activity_date_end),
			'is_publish' => ''.$is_publish.'',
			'update_by' => $this->session->userdata('username'),
			'create_date' => $datetime,
			'update_date' => $datetime
		);
		$this->db->insert('calendar',$data);
		$calendar_id = $this->db->insert_id();
		
		//Insert calendar_info
		$data = array(
			'calendar_id' => $calendar_id,
			'calendar_subject' => $calendar_subject,
			'calendar_brief' => $calendar_brief,			
			'calendar_detail' => $calendar_detail,
			'calendar_place' => $calendar_place,
			'calendar_link' => $calendar_link	
		);
		$this->db->insert('calendar_info',$data);
			
		//Insert calendar_image
		$dataImage = array();
		if(!empty($uploadify_store[0])) foreach($uploadify_store as $key => $value):
			//Resize Image
			$data = imageResize(array(
				'id' => $calendar_id,
				'source_image' => $this->temp_folder.$value,
				'new_path' => $this->upload_folder.'calendar/images/',
				'dimension' => array(												
					array(
						'width' => 120,
						'height' => 120
					)					
				)
			));			
			
			//delete temp
			@unlink($this->temp_folder.$value);
			
			$dataImage[] = array(
				'calendar_id' => $calendar_id,
				'calendar_file_name' => $data->file_name,
				'calendar_file_ext' => $data->file_ext
			);	
		endforeach;
		if(!empty($dataImage)) $this->db->insert_batch('calendar_image',$dataImage);
		
		//Delete Memcache
		$this->memcache->delete('dataCalendar');
		$this->memcache->delete('dataModuleCalendarEvent');
		
		//Redirect to Edit Page
		redirect('calendar/content_management/edit/'.$calendar_id);		
	}
	
	function content_management_edit($calendar_id)
	{
		$calendar_type_id = $this->input->post('calendar_type_id');
		$calendar_activity_date_start = $this->input->post('calendar_activity_date_start');
		$calendar_activity_date_end = $this->input->post('calendar_activity_date_end');
		$is_publish = $this->input->post('is_publish');
		$calendar_subject = $this->input->post('calendar_subject');
		$calendar_brief = $this->input->post('calendar_brief');
		$calendar_detail = $this->input->post('calendar_detail');
		$calendar_place = $this->input->post('calendar_place');
		$calendar_link = $this->input->post('calendar_link');
		$uploadify_store = explode(',',$this->input->post('uploadify_store'));
		$datetime = date('Y-m-d H:i:s');

		//Insert to calendar
		$data = array(
			'calendar_activity_date_start' => convertDatetime($calendar_activity_date_start),
			'calendar_activity_date_end' => convertDatetime($calendar_activity_date_end),
			'is_publish' => ''.$is_publish.'',
			'update_by' => $this->session->userdata('username'),
			'update_date' => $datetime
		);
		$this->db->update('calendar',$data,array('calendar_id' => $calendar_id));
		
		//Update calendar_info
		$data = array(
			'calendar_subject' => $calendar_subject,
			'calendar_brief' => $calendar_brief,			
			'calendar_detail' => $calendar_detail,
			'calendar_place' => $calendar_place,
			'calendar_link' => $calendar_link
		);
		$this->db->update('calendar_info',$data,array('calendar_id' => $calendar_id));
		
		//Insert calendar_image
		$dataImage = array();
		if(!empty($uploadify_store[0])) foreach($uploadify_store as $key => $value):
			//Resize Image
			$data = imageResize(array(
				'id' => $calendar_id,
				'source_image' => $this->temp_folder.$value,
				'new_path' => $this->upload_folder.'calendar/images/',
				'dimension' => array(											
					array(
						'width' => 120,
						'height' => 120
					)				
				)
			));		
			
			//delete temp
			@unlink($this->temp_folder.$value);
			
			$dataImage[] = array(
				'calendar_id' => $calendar_id,
				'calendar_file_name' => $data->file_name,
				'calendar_file_ext' => $data->file_ext
			);	
		endforeach;
		if(!empty($dataImage)) $this->db->insert_batch('calendar_image',$dataImage);
		
		//Delete Memcache
		$this->memcache->delete('dataCalendar');
		$this->memcache->delete('dataModuleCalendarEvent');
	}
	
	function ajax_update_image()
	{
		$calendar_image_id = $this->input->post('calendar_image_id');
		if(!empty($calendar_image_id)) foreach($calendar_image_id as $key => $value):
			$this->db->update('calendar_image',array('calendar_image_order' => $key),array('calendar_image_id' => $value));
		endforeach;
		exit;
	}
	
	function ajax_delete_image($calendar_image_id)
	{
		$dataImage = $this->db->get_where('calendar_image',array('calendar_image_id' => $calendar_image_id),1)->row();
		$this->db->delete('calendar_image',array('calendar_image_id' => $calendar_image_id));
		//Delete All Image
		$map = directory_map($this->upload_folder.'calendar/images/', 1);
		foreach($map as $folder):
			@unlink($this->upload_folder.'calendar/images/'.$folder.'/'.$dataImage->calendar_file_name.'.'.$dataImage->calendar_file_ext);
		endforeach;
		exit;
	}	
	
	function content_management_delete()
	{
		$id = $this->input->post('id');
		$map = directory_map($this->upload_folder.'calendar/images/', 1);
		foreach($id as $value):
			$dataImage = $this->db->get_where('calendar_image',array('calendar_id' => $value))->result();
			$tables = array('calendar','calendar_info','calendar_image');
			$this->db->where('calendar_id', $value);
			$this->db->delete($tables);
			//Delete All Image
			foreach($map as $folder):
				foreach($dataImage as $items):
					@unlink($this->upload_folder.'calendar/images/'.$folder.'/'.$items->calendar_file_name.'.'.$items->calendar_file_ext);
				endforeach;
			endforeach;					
		endforeach;	

		//Delete Memcache
		$this->memcache->delete('dataCalendar');
		$this->memcache->delete('dataModuleCalendarEvent');				
	}
	/*End Content Management*/
}

?>